<?php
Yii::import('zii.widgets.CListView');

class CAuctionsPool extends CListView
{
    public $template = '{items}{pager}';
    
    public $shelfUrl="";
    
    public function init()
    {
        $this->shelfUrl=!empty($this->shelfUrl)?$this->shelfUrl:Yii::app()->baseUrl."/ajax/shelfData/";
        $this->itemsTagName = 'ul';
        $this->itemsCssClass = 'auction-pool-item';
        $this->htmlOptions['class'] = 'auction-pool';
        $this->pagerCssClass = 'auction-pool-pager';
        $this->pager = array(
            'class' => 'application.components.widgets.auctionPool.pagers.AuctionPoolPager',
            'header' => '',
            'prevPageLabel' => "<",
            'nextPageLabel' => '>',
            'maxButtonCount' => 5,
            'previousPageCssClass' => 'prev',
            'nextPageCssClass' => 'next',
        );
        $this->buildJs();
        parent::init();
    }

    public function getSoldHtml(){
        $html='<div><img src="".Yii::app()->baseUrl."/images/sold.png" /></div>';
        return $html;
    }
    
    protected function buildJs()
    {
        Yii::app()->clientScript->registerScriptFile(Yii::app()->resource->path('js') . "/client.js");
        $auctionPoolJsConfig = array(
            'template' => $this->getItemView(),
            'limit' =>  $this->dataProvider->model->limit,
            'checkData' => array('search_category_id' => $this->dataProvider->model->search_category_id)
        );
        $auctionPoolJsConfig = CJavaScript::encode($auctionPoolJsConfig);
        $noAvatar = Yii::app()->resource->path('img').'/no-avatar.png';
        $auctionPoolJs = "
        	jQuery('#{$this->getId()} .{$this->itemsCssClass}').shelf({$auctionPoolJsConfig}).initDomData();
        	jQuery('#{$this->getId()} .{$this->itemsCssClass}').shelf().auctionDom().setBaseEvent('winner_name.dom.setAttributeAfter',function(value, attribute, attributeDom, auctionDom){ 
               if(value != ''){
                    auctionDom.find('.winner-no-customer').addClass('hide');
                    auctionDom.find('.winner-customer').removeClass('hide');
               }
               
            });
            jQuery('#{$this->getId()} .{$this->itemsCssClass}').shelf().auctionDom().setBaseEvent('winner_price.dom.setFormat',function(value){
                return '$'+value;
            })
            
            jQuery('#{$this->getId()} .{$this->itemsCssClass}').shelf().auctionDom().setBaseEvent('winner_avatar.dom.setAttributeBefore',function(value, attribute, attributeDom, auctionDom){
                attributeDom.on('error',function(){
		          $(this).attr('src','{$noAvatar}');
		        });
                return true;
            });
            
        ";
        
        if($this->dataProvider->modelClass=="AuctionInfo"){         
        }elseif ($this->dataProvider->modelClass=="AuctionPoolProvider"){
            Yii::app()->clientScript->registerScript('auctionPool_' . $this->getId(), $auctionPoolJs);
            Yii::app()->clientScript->registerScript('auctionPoolTimer',"client.engine.
            config('engineAuctionInfo.url', '".Yii::app()->baseUrl."/ajax/auctionData/').
            config('engineShelfInfo.url', '".$this->shelfUrl."').
            run();
            ");
        }
         

        $this->afterAjaxUpdate = "js:function(id,data){
			jQuery('#{$this->getId()} .{$this->itemsCssClass}').shelf().initDomData();
		}";



    }

    protected function getItemView()
    {
        $owner = $this->getOwner();
        $viewFile = $owner->getViewFile($this->itemView);
        return $owner->renderFile($viewFile, array('data' => new stdClass()), true);
    }

}